<template>
  <div class="home">
    <div class="swiper-container">
      <div class="swiper-wrapper">
          <div
            v-for="banner in banners"
            :key="`banner-${banner.id}`"
            class="swiper-slide"
          >
            <img :src="banner.imageUrl" :title="banner.title">
          </div>
      </div>
      <!-- 如果需要分页器 -->
      <div class="swiper-pagination"></div>
    </div>
    <van-grid square>
      <van-grid-item
        is-link
        @click="showPopup(tab.id)"
        v-for="tab in tabs"
        :key="`tab-${tab.id}`"
        :icon="tab.imageUrl"
        clickable=true
        :text="tab.name" />
        <van-popup v-model="show" round position="top">
          <div class="lists">
            <ul class="list">
              <router-link
                tag="li"
                :to="`/listPage?url=${list.url}`"
                v-for="list in lists"
                :key="`list-${list.id}`"
              >
                <img :src="list.imageUrl" alt="">
                <span>{{ list.title }}</span>
              </router-link>
            </ul>
          </div>
        </van-popup>
    </van-grid>
    <!-- 热卖商品-->
    <div class="hotShop">
      <div class="hotShop-left">
        <span>HOT</span>
        <span>热销排行</span>
      </div>
      <div class="hotShop-right">
        <HomeHotShop
          v-for="shop in hotShops"
          :key="`shop-${shop.id}`"
          :id="shop.id"
          :image="shop.image"
          :title="shop.title"
          :price="shop.price"
        />
      </div>
    </div>
    <!-- 小编推荐 -->
    <div class="homeListShop">
      <HomeListShop/>
    </div>
  </div>
</template>

<script>
import Swiper from 'swiper'
import 'swiper/css/swiper.min.css'
import HomeHotShop from '@/components/HomeHotShop'
import HomeListShop from '@/components/HomeListShop'
export default {
  name: 'Home',
  data () {
    return {
      banners: [],
      tabs: [],
      show: false,
      lists: [],
      hotShops: []
    }
  },
  components: {
    HomeHotShop,
    HomeListShop
  },
  created () {
    this.$http.getLists(1).then(resp => {
      // console.log(resp)
      this.banners = resp.banners
      this.hotShops = resp.topList
      // 在nextTick里面执行代码
      this.$nextTick().then(() => {
        this.initSwiper()
      })
    })
    this.$http.getTabs().then(resp => {
      this.tabs = resp.list.slice(1, 9)
      // console.log(this.tabs)
    })
  },
  methods: {
    initSwiper () {
      // eslint-disable-next-line
      new Swiper('.swiper-container', {
        loop: true,
        autoplay: true,
        pagination: {
          el: '.swiper-pagination'
        }
      })
    },
    showPopup (id) {
      this.show = true
      this.$http.getLists(id).then(resp => {
        this.lists = resp.categories
        // console.log(resp.categories)
      })
    }
  },
  deactivated () {
    // console.log('deactivated')
  },
  activated () {
    // console.log('activated')
  }
}
</script>
<style lang="scss">
.home {
  background: #f5f5f5;
}
.swiper-container {
  width: 100%;
  height: 0;
  padding-top: 378 / 1008 * 100%;
  position: relative;
  .swiper-wrapper {
    position: absolute;
    left: 0;
    top: 0;
  }
  img {
    width: 100%;
  }
}
.van-grid-item__icon {
  font-size: 400% !important;
}
.lists {
  width: 100%;
  .list {
    display: flex;
    flex-wrap: wrap;
    justify-content: flex-start;
    li {
      display: flex;
      flex-direction: column;
      align-items: center;
      width: 25%;
      padding: 4%;
      box-sizing: border-box;
      img {
        width: 100%;
      }
      span {
        font-size: 14px;
      }
    }
  }
}
.hotShop {
  width: 100%;
  background-color: white;
  padding: 10px 0;
  box-sizing: border-box;
  margin-top: 10px;
  display: flex;
  overflow: auto;
  &-left {
    width: 62px;
    padding: 0 10px;
    height: 130px;
    background: #fee44e;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    font-size: 14px;
    span:nth-child(1) {
      font-weight: 600;
      display: block;
      border-bottom: 2px solid #987b2a;
      margin-bottom: 5px;
      box-sizing: border-box;
      padding-bottom: 8px;
      color: #7f6221;
    }
    span:nth-child(2) {
      width: 30px;
      text-align: center;
      color: #b99d35;
    }
  }
  &-right {
    flex: 1;
    display: flex;
    justify-content: space-around;
    font-size: 14px;
    margin-left: 10px;
  }
}
</style>
